package com.topvision.auth.service.impl;

import com.topvision.auth.domain.SysRole;
import com.topvision.auth.domain.SysUser;
import com.topvision.auth.domain.SysUserAccount;
import com.topvision.auth.dto.RegDTO;
import com.topvision.auth.repository.SysRoleRepository;
import com.topvision.auth.repository.SysUserAccountRepository;
import com.topvision.auth.repository.SysUserRepository;
import com.topvision.auth.service.AuthService;
//import com.topvision.common.mail.MailService;

import com.topvision.auth.util.mail.MailService;
import com.topvision.utils.base.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private SysUserAccountRepository sysUserAccountRepository;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired
    private MailService mailService;

    @Override
    public JsonResult<SysUser> register(RegDTO userToAdd) {
        SysUser sysUser = new SysUser();
        sysUser.setEmail(userToAdd.getEmail());
        sysUser.setMobile(userToAdd.getMobile());
        sysUser.setUsername(userToAdd.getEmail());

        SysRole sysRole = sysRoleRepository.findByName("ROLE_USER");
        if(sysRole == null){
            sysRole = new SysRole();
            sysRole.setName("ROLE_USER");
            sysRole.setValue("USER");
            sysRole = sysRoleRepository.save(sysRole);
        }
        Set<SysRole> roles = new HashSet<>();
        roles.add(sysRole);
        sysUser.setRoles(roles);
        sysUserRepository.save(sysUser);

        SysUserAccount account = new SysUserAccount();
        account.setAccount(userToAdd.getEmail());
        account.setPassword(encoder.encode(userToAdd.getPassword()));
        account.setUserId(sysUser.getId());

        sysUserAccountRepository.save(account);
        String content = "用户注册成功，账号为 ： " + account.getAccount() + ",密码为 ：" + userToAdd.getPassword();

        mailService.sendSimpleMail(account.getAccount(),"Topvision 注册系统通知",content);


        return  JsonResult.<SysUser>builder().data(sysUser).build();
    }

    @Override
    public JsonResult<String> login(String username, String password) {
        return null;
    }

    @Override
    public String refresh(String oldToken) {
        return null;
    }

    @Override
    public SysUserAccount checkAccount(String account) {
        SysUserAccount userAccount = sysUserAccountRepository.findOneByAccount(account);
        return userAccount;
    }

    @Override
    public void restPassword(String account) {
        SysUserAccount userAccount = sysUserAccountRepository.findOneByAccount(account);
        userAccount.setPassword(encoder.encode("123456"));
        sysUserAccountRepository.save(userAccount);
    }


}
